﻿using Microsoft.AspNetCore.Mvc;
using yunbo.Model.BI;
using yunbo.Repositorys.BIRepositorys;

namespace yunbo.Controllers.BIControllers
{
	[Route("api/[controller]")]
	[ApiController]
	public class WLController : Controller
	{
		WLRepository wlRepository = new WLRepository();


		/// <summary>
		/// 获取物料
		/// </summary>
		/// <param name="state"></param>
		/// <param name="str"></param>
		/// <returns></returns>
		[HttpGet("GetWL")]
		public IActionResult GetWL(int state, string? str)
		{
			List<WL> wl = wlRepository.GetWL(state, str);
			return Ok(wl);
		}


		/// <summary>
		/// 改变状态
		/// </summary>
		/// <param name="id"></param>
		/// <param name="state"></param>
		/// <returns></returns>
		[HttpPut("StateChange")]
		public IActionResult StateChange(int id, int state)
		{
			bool res = wlRepository.StateChange(id, state);
			return Ok(res);
		}


		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="wl"></param>
		/// <returns></returns>
		[HttpPost("AddWL")]
		public IActionResult AddWL(WL wl)
		{
			// 数据验证
			if (string.IsNullOrEmpty(wl.wl_code))
			{
				return BadRequest("物料编码不能为空");
			}
			if (string.IsNullOrEmpty(wl.wl_name))
			{
				return BadRequest("物料名称不能为空");
			}
			if (wl.wl_fl <= 0)
			{
				return BadRequest("请选择物料分类");
			}
			if (string.IsNullOrEmpty(wl.wl_size))
			{
				return BadRequest("物料规格不能为空");
			}
			if (wl.wl_dw <= 0)
			{
				return BadRequest("请选择物料单位");
			}
			if (wl.wl_type <= 0)
			{
				return BadRequest("请选择物料类型");
			}
			if (wl.wl_ly <= 0)
			{
				return BadRequest("请选择物料来源");
			}

			bool res = wlRepository.AddWL(wl);
			return Ok(res);
		}


		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="wl"></param>
		/// <returns></returns>
		[HttpPut("UpdateWL")]
		public IActionResult UpdateWL(WL wl)
		{

			// 数据验证
			if (string.IsNullOrEmpty(wl.wl_code))
			{
				return BadRequest("物料编码不能为空");
			}
			if (string.IsNullOrEmpty(wl.wl_name))
			{
				return BadRequest("物料名称不能为空");
			}
			if (wl.wl_fl <= 0)
			{
				return BadRequest("请选择物料分类");
			}
			if (string.IsNullOrEmpty(wl.wl_size))
			{
				return BadRequest("物料规格不能为空");
			}
			if (wl.wl_dw <= 0)
			{
				return BadRequest("请选择物料单位");
			}
			if (wl.wl_type <= 0)
			{
				return BadRequest("请选择物料类型");
			}
			if (wl.wl_ly <= 0)
			{
				return BadRequest("请选择物料来源");
			}

			bool res = wlRepository.UpdateWL(wl);
			return Ok(res);
		}


		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPut("DelWL")]
		public IActionResult DelWL(int id)
		{
			bool res = wlRepository.DelWL(id);
			return Ok(res);
		}


		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPut("DelWLs")]
		public IActionResult DelWLs(int[] ids)
		{
			bool res = wlRepository.DelWLs(ids);
			return Ok(res);
		}

        [HttpGet("CGWL")]
        public IActionResult CGWL()
        {
            List<WL> wL = wlRepository.CHWL();
            return Ok(wL);
        }


    }
}
